
<template>
  <el-dialog title="重置密码" :visible.sync="dialogVisiblep" :destroy-on-close="true" :close-on-click-modal="false"
    width="500px" :before-close="handleClose">
    <el-form ref="form" :model="form" label-width="100px" :rules="rules">
      <el-form-item label="用户名：" prop="username">
        <el-input size="small" v-model="form.username" disabled style="width:330px;"></el-input>
      </el-form-item>
      <el-form-item label="重置密码：" prop="password">
        <el-input size="small" v-model="form.password" type="password" style="width:330px;" show-password></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel('form')">取 消</el-button>
      <el-button type="primary" @click="resetPSW('form')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: "passwordForm",
  methods: {
    init(v) {
      this.id = v.id
      this.form.username = v.username
      this.dialogVisiblep = true
    },
    cancel(form) {
      this.$refs[form].resetFields();
      this.dialogVisiblep = false
    },
    handleClose(done) {
      this.$refs["form"].resetFields();
      done()
    },
    resetPSW(form) {
      this.$refs[form].validate(async (valid) => {
        if (valid) {
          let params = {
            id: this.id,
            password: this.form.password,
          };
          const res = await this.$api.resetPasswords(params)
          if (res.code == 200) {
            this.$message.success("重置成功");
            this.dialogVisiblep = false;
          }
          // this.$api.resetPasswords(params).then((res) => {
          //   this.$message.success("重置成功");
          //   this.dialogVisiblep = false;
          // });
        } else {
          return false;
        }
      })
    }
    // validate() {
    //   return new Promise((res, rej) => {
    //     this.$refs.form.validate((v) => {
    //       if (v) {
    //         console.log(v);
    //         res(v)
    //       } else {
    //         rej(v)
    //       }
    //     })
    //   })

    // },
  },

  data() {
    var validatePass = (rule, value, callback) => {
      //必须包含大小写字母、数字、特殊字符长度再8-16位之间
      var regex = new RegExp("(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,16}");
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (value.length < 8 || value.length > 16) {
        callback(new Error("请输入8~16位密码"));
      } else if (!regex.test(value)) {
        callback(new Error("密码必须同时包含字母、数字和特殊字符其中三项且至少8位"));
      } else {
        callback();
      }
    };
    return {
      rules: {
        password: [{ validator: validatePass, trigger: "blur" }],
        username: [{ required: true, message: '不可为空' }],
      },
      type: 'add',
      roleList: [],
      dialogVisiblep: false,
      id: '',
      form: {
        password: '',
        username: '',
        id: ''
      },
    }
  },
}
</script>

<style scoped></style>
